
package com.platform.server.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.platform.server.dao.DiscountDao;
import com.platform.server.dao.RoyaltyDao;
import com.platform.server.service.DiscountService;
import com.platform.server.service.VipSettingService;
import com.szrca.ima.utils.StringTools;

@Controller
public class DiscountController {

	@Autowired
	private DiscountDao discountDao;

	@Autowired
	private DiscountService discountService;

	@Autowired
	private VipSettingService vipSettingService;
	@Autowired
	private RoyaltyDao royaltyDao;

	/**
	 * 查看普通优惠券设置
	 */
	@RequestMapping("/selectCommontDiscount")
	public Object selectCommontDiscount(HttpServletRequest request) {
		// 查看普通优惠券设置/*-
		List<Map<String, Object>> lists = discountDao.selectCommontDiscount();
		// 查看活动优惠券设置
		List<Map<String, Object>> list = discountDao.selectActivityDiscount();
		request.setAttribute("lists", lists);
		request.setAttribute("list", list);
		request.setAttribute("listc", discountDao.selectDiscount());
		request.setAttribute("activity", list.get(0));
		List<Map<String, Object>> selectSetting = vipSettingService.selectSetting(null, null, null, null, null);
		request.setAttribute("selectSetting", selectSetting);
		return "/ui/discount/selectCommontDiscount.jsp";
	}

	/**
	 * 设置优惠券张数
	 */
	@RequestMapping("/updateDiscontNum")
	public void updateDiscontNum(String common_discount_id, String five_ticket, String ten_ticket, String twenty_ticket,
			String fifty_ticket, String hundred_ticket, String count_money, HttpServletResponse response)
					throws Exception {
		if (StringTools.isEmpty(five_ticket))
			five_ticket = "0";
		if (StringTools.isEmpty(ten_ticket))
			ten_ticket = "0";
		if (StringTools.isEmpty(twenty_ticket))
			twenty_ticket = "0";
		if (StringTools.isEmpty(fifty_ticket))
			fifty_ticket = "0";
		if (StringTools.isEmpty(hundred_ticket))
			hundred_ticket = "0";
		int flag = discountService.updateDiscontNum(common_discount_id, five_ticket, ten_ticket, twenty_ticket,
				fifty_ticket, hundred_ticket, count_money);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
		response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}

	/**
	 * 设置普通优惠券
	 */
	@RequestMapping("/setCommontDiscount")
	public void setCommontDiscount(HttpServletRequest request, String recharge_money, String present_money,
			String service_life, HttpServletResponse response) throws Exception {
		recharge_money = recharge_money.substring(0, recharge_money.length() - 1);
		present_money = present_money.substring(0, present_money.length() - 1);
		service_life = service_life.substring(0, service_life.length() - 1);
		String[] split1 = recharge_money.split(",");
		String[] split2 = present_money.split(",");
		String[] split3 = service_life.split(",");
		Map<String, Object> map = new HashMap<>();
		map.put("recharge_money1", split1[0]);
		map.put("recharge_money2", split1[1]);
		map.put("recharge_money3", split1[2]);
		map.put("recharge_money4", split1[3]);
		map.put("present_money1", split2[0]);
		map.put("present_money2", split2[1]);
		map.put("present_money3", split2[2]);
		map.put("present_money4", split2[3]);
		map.put("service_life1", split3[0]);
		map.put("service_life2", split3[1]);
		map.put("service_life3", split3[2]);
		map.put("service_life4", split3[3]);
		int flag = discountService.setCommontDiscount(map);
		discountDao.updateStatus(request.getParameter("status"), request.getParameter("id"));
		List<Map<String, Object>> selectDiscount = discountDao.selectDiscount();
		for (Map<String, Object> map2 : selectDiscount) {
			if (!map2.get("id").toString().equals(request.getParameter("id"))) {
				discountDao.updateStatus(request.getParameter("status").equals("0") ? "1" : "0",
						map2.get("id").toString());
			}
		}
		discountDao.updateStatus(request.getParameter("status"), request.getParameter("id"));
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
		response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}

	/**
	 * 查看充值面额
	 */
	@RequestMapping("/selectRechargeSet")
	public Object selectRechargeSet(HttpServletRequest request) {
		List<Map<String, Object>> lists = discountDao.selectRechargeSet();
		request.setAttribute("lists", lists);
		return "/ui/discount/recharge_set.jsp";
	}

	/**
	 * 设置充值面额
	 */
	@RequestMapping("/rechargeSet")
	public void rechargeSet(HttpServletResponse response, String five_ticket, String ten_ticket, String twenty_ticket,
			String fifty_ticket, String hundred_ticket) throws Exception {
		int flag = discountService.rechargeSet(five_ticket, ten_ticket, twenty_ticket, fifty_ticket, hundred_ticket);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
		response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}

	/**
	 * 设置活动优惠券张数
	 */
	@RequestMapping("/updateActivityDiscontNum")
	public void updateActivityDiscontNum(String activity_discount_id, String five_ticket, String ten_ticket,
			String twenty_ticket, String fifty_ticket, String hundred_ticket, String count_money,
			HttpServletResponse response) throws Exception {
		if (StringTools.isEmpty(five_ticket))
			five_ticket = "0";
		if (StringTools.isEmpty(ten_ticket))
			ten_ticket = "0";
		if (StringTools.isEmpty(twenty_ticket))
			twenty_ticket = "0";
		if (StringTools.isEmpty(fifty_ticket))
			fifty_ticket = "0";
		if (StringTools.isEmpty(hundred_ticket))
			hundred_ticket = "0";
		int flag = discountService.updateActivityDiscontNum(activity_discount_id, five_ticket, ten_ticket,
				twenty_ticket, fifty_ticket, hundred_ticket, count_money);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
		response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}

	/**
	 * 设置活动优惠券
	 */
	@RequestMapping("/setActivityDiscount")
	public void setActivityDiscount(HttpServletRequest request, String recharge_money, String present_money,
			String service_life, HttpServletResponse response, String start_time, String end_time) throws Exception {
		recharge_money = recharge_money.substring(0, recharge_money.length() - 1);
		present_money = present_money.substring(0, present_money.length() - 1);
		service_life = service_life.substring(0, service_life.length() - 1);
		String[] split1 = recharge_money.split(",");
		String[] split2 = present_money.split(",");
		String[] split3 = service_life.split(",");
		Map<String, Object> map = new HashMap<>();
		map.put("recharge_money1", split1[0]);
		map.put("recharge_money2", split1[1]);
		map.put("recharge_money3", split1[2]);
		map.put("recharge_money4", split1[3]);
		map.put("present_money1", split2[0]);
		map.put("present_money2", split2[1]);
		map.put("present_money3", split2[2]);
		map.put("present_money4", split2[3]);
		map.put("service_life1", split3[0]);
		map.put("service_life2", split3[1]);
		map.put("service_life3", split3[2]);
		map.put("service_life4", split3[3]);
		map.put("service_life4", split3[3]);
		map.put("start_time", start_time);
		map.put("end_time", end_time);
		int flag = discountService.setActivityDiscount(map);
		discountDao.updateStatus(request.getParameter("status"), request.getParameter("id"));
		List<Map<String, Object>> selectDiscount = discountDao.selectDiscount();
		for (Map<String, Object> map2 : selectDiscount) {
			if (!map2.get("id").toString().equals(request.getParameter("id"))) {
				discountDao.updateStatus(request.getParameter("status").equals("0") ? "1" : "0",
						map2.get("id").toString());
			}
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
		response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}

	/**
	 * 平台费用设置
	 */
	@RequestMapping("/platformDiscount")
	public Object platformDiscount(HttpServletRequest request) {
		List<Map<String, Object>> lists = discountDao.platformDiscount();
		Double d = (Double) lists.get(0).get("discount");
		lists.get(0).put("discount", d.intValue());
		request.setAttribute("lists", lists);
		List<Map<String, Object>> selectDiscount = discountDao.selectDiscount();
		for (Map<String, Object> map : selectDiscount) {
			if(map.get("id").toString().equals("3")){
				request.setAttribute("listc", map);
			}
		}
		// 提现开关
		Integer flag = discountDao.select();
		request.setAttribute("flag", flag);
		return "/ui/discount/platform_discount.jsp";
	}

	
	
	
	
	
	/**
	 * 修改平台费用设置
	 * 
	 * @throws JSONException
	 */
	@RequestMapping("/updatePlatformDiscount")
	public void updatePlatformDiscount(HttpServletResponse response, Double discount,Integer on_off) throws Exception {
		int flag = discountDao.updatePlatformDiscount(discount);
		// 修改开关
	    discountDao.updateOnOff(on_off);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
  
		response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
		
		
	}
	
	/**
	 * 修改平台费用设置
	 * @throws JSONException 
	 */
	@RequestMapping("/updateApp")
	public void updateApp(HttpServletResponse response,String id,String status) throws Exception{
		int flag = discountDao.updateStatus(status,id);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag",flag);
		response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}

	/**
	 * 提成设置
	 */
	@RequestMapping("/royalty")
	public Object royalty(HttpServletRequest request) {
		Map<String, Object> lists = royaltyDao.selectRoyal();
		request.setAttribute("lists", lists);
		return "/ui/discount/royal.jsp";
	}

	/**
	 * 提成设置
	 * 
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 * @throws JSONException
	 */
	@RequestMapping("/updateRoyalty")
	public void updateRoyalty(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("vip_one_level", request.getParameter("vip_one_level"));
		map.put("vip_ent_level", request.getParameter("vip_ent_level"));
		map.put("vip_two_level", request.getParameter("vip_two_level"));
		map.put("ordinary_two_level", request.getParameter("ordinary_two_level"));
		map.put("ordinary_ent_level", request.getParameter("ordinary_ent_level"));
		map.put("ordinary_one_level", request.getParameter("ordinary_one_level"));
		map.put("vip_discount", request.getParameter("vip_discount"));
		map.put("vip_user_discount", request.getParameter("vip_user_discount"));
		map.put("ent_user_discount", request.getParameter("ent_user_discount"));
		map.put("two_vip_one", request.getParameter("two_vip_one"));
		map.put("two_vip_two", request.getParameter("two_vip_two"));
		map.put("ent_pay", request.getParameter("ent_pay"));
		map.put("two_vip_discount", request.getParameter("two_vip_discount"));
		map.put("two_user_discount", request.getParameter("two_user_discount"));
		map.put("vip_recommend_one", request.getParameter("vip_recommend_one"));
		map.put("vip_recommend_ent", request.getParameter("vip_recommend_ent"));
		map.put("twovip_recommend_one", request.getParameter("twovip_recommend_one"));
		map.put("twovip_recommend_two", request.getParameter("twovip_recommend_two"));
		map.put("twovip_recommend_ent", request.getParameter("twovip_recommend_ent"));
		map.put("id", request.getParameter("id"));
		int flag = royaltyDao.updateRoyal(map);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
		response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
}
